﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2011-05-04
//
// Update Date : 
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


class F3DGrid : public IFMemObject
{
    FDECLARE_CLASS(F3DGrid)

public:
    // X轴向格子数量，Y轴格子数量，单元格尺寸（单位米）
    F3DGrid(int w_count, int h_count, float cell_size)
    {
        float sx = -w_count * cell_size / 2.0f;
        float sy = -h_count * cell_size / 2.0f;

        vBuffer.clear();
        vBuffer.reserve(w_count * h_count);
        FVERTEX3D_LINE line;
        line.z = -0.001f;
        for (int y = 0; y <= h_count; y++)
        {
            DWORD v = DWORD(y * 255.0f / (float)h_count);
            line.color = 0x40000000 | ((v & 0xFF) << 8);
            if (y == h_count / 2)
                line.color = 0x80FFFF00;

            line.x = sx + 0;
            line.y =sy + y * cell_size;
            vBuffer.push_back(line);
            line.x = sx + w_count * cell_size;
            line.y = sy + y * cell_size;
            vBuffer.push_back(line);
        }

        for (int x = 0; x <= w_count; x++)
        {
            DWORD v = DWORD(x * 255.0f / (float)w_count);
            line.color = 0x40000000 | ((v & 0xFF) << 16);
            if (x == w_count / 2)
                line.color = 0x8FFFFF00;

            line.x = sx + x * cell_size;
            line.y = sy + 0;
            vBuffer.push_back(line);
            line.x = sx + x * cell_size;
            line.y = sy + h_count * cell_size;
            vBuffer.push_back(line);
        }
    }
    ~F3DGrid()
    {
        vBuffer.release();
    }
    void Render(IFEngine* fge, bool axis, FVECTOR3 point = FVECTOR3())
    {
        if (!fge)
            return;

        fge->GetRender()->DrawLine3D(vBuffer.address(), vBuffer.size()>>1);

        if (axis)
        {
            FVECTOR3 ex(1,0,0), ey(0,1,0), ez(0,0,1);
            ex += point;
            ey += point;
            ez += point;
            fge->GetRender()->DrawLine3D(&point, &ex, 0xFFFF0000);
            fge->GetRender()->DrawLine3D(&point, &ey, 0xFF00FF00);
            fge->GetRender()->DrawLine3D(&point, &ez, 0xFF0000FF);

            float x, y;
            float font_half = fge->GetFont()->GetFontData(eCD_DefFontID)->nFontSize / 2.0f;
            if (fge->GetCamera()->WorldToScreen(&ex, x, y))
                fge->GetFont()->DrawTextEx(x + 2, y - font_half, _FTW("+X"), eCD_DefFontID, 0xFFFF0000, eFE_Fringe, 0xFFFFFFFF);
            if (fge->GetCamera()->WorldToScreen(&ey, x, y))
                fge->GetFont()->DrawTextEx(x + 2, y - font_half, _FTW("+Y"), eCD_DefFontID, 0xFF00FF00, eFE_Fringe, 0xFFFFFFFF);
            if (fge->GetCamera()->WorldToScreen(&ez, x, y))
                fge->GetFont()->DrawTextEx(x + 2, y - font_half, _FTW("+Z"), eCD_DefFontID, 0xFF0000FF, eFE_Fringe, 0xFFFFFFFF);
        }
    }

protected:
    FMemoryBlock<FVERTEX3D_LINE> vBuffer;
};